package com.heima.test14;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Demo01 {
    public static void main(String[] args) throws IOException {
        //1 客户端  注意构造里传入的是要连接的服务器的地址和端口
        Socket socket = new Socket("127.0.0.1", 10000);
        //2连接服务器
        //socket.connect(new InetSocketAddress("127.0.0.1",10000));
        //2 获取发送数据的 输出流对象
        OutputStream os = socket.getOutputStream();
        String s = "hello  你好";
        os.write(s.getBytes());

        //3 获取接收数据的 输入流对象
        InputStream is = socket.getInputStream();
        byte[] bytes = new byte[1024];
        System.out.println("111");
        int len = is.read(bytes);//接收数据 这里会等待
        System.out.println("222");
        //打印接收的数据
        System.out.println(new String(bytes, 0, len));

        is.close();//关闭流
        os.close();//关闭输出流
        //断开连接
        socket.close();


    }
}
